//
//  YBCloudComputerPayCell.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/30.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit

class YBCloudComputerPayCell: UITableViewCell {
    
    /// selectedTypeBlock
    var selectedTypeBlock: ((_ selectedType: Int) -> Void)?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        self.backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        let line1 = UIView(color: kRGBColor(191, 190, 190))
        let line2 = UIView(color: kRGBColor(191, 190, 190))
        
//        let arr: [YBCloudComputerPayItem.ItemType] = [.wechatPay, .aliPay, .apple]
        let arr: [YBCloudComputerPayItem.ItemType] = [.apple]
        
        arr.forEach { (type) in
            let item = self.createItem(for: type)
            stackView.addArrangedSubview(item)
        }
        
        contentView.addSubview(bgView)
        bgView.addSubviews([titleLabel, stackView, line1, line2])
        
        bgView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.top.equalTo(12)
            make.height.equalTo(22)
        }
        
        stackView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.height.equalTo(UIDevice.bk_isiPhone ? 44*arr.count : 60*arr.count)
            make.bottom.equalTo(-4)
        }
        
        line1.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.height.equalTo(0.3)
            make.bottom.equalTo(stackView.snp.top)
        }
        
        line2.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.centerY.equalTo(stackView)
            make.height.equalTo(0.3)
        }
        
    }
    
    /// 重写frame
    override var frame: CGRect {
        get { return super.frame }
        set {
            var frame = newValue
            frame.origin.x += 10
            frame.size.width -= 2*10
            super.frame = frame
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var bgView: UIView = {
        let view = UIView(color: .lightWhiteDark27)
        view.bk_addCornerBorder(radius: 4, borderWidth: 0.1, borderColor: .clear)
        return view
    }()
    
    private lazy var titleLabel = self.bk_addLabel(text: "请选择支付方式：", font: .systemFont(ofSize: 14, weight: .medium), bgColor: .clear, textColor: .lightBlack51DarkLight230)
    
    private lazy var stackView: UIStackView = {
        let _stackView = UIStackView()
        _stackView.axis = .vertical
        _stackView.spacing = 0
        _stackView.alignment = .fill
        _stackView.distribution = .fillEqually
        return _stackView
    }()
    
}

// MARK: - Private
extension YBCloudComputerPayCell {
    
    private func createItem(for type: YBCloudComputerPayItem.ItemType) -> YBCloudComputerPayItem {
        let item = YBCloudComputerPayItem(type: type)
        item.isUserInteractionEnabled = true
        item.tag = type.rawValue
        let tap = UITapGestureRecognizer(target: self, action: #selector(selectItem(_:)))
        item.addGestureRecognizer(tap)
        return item
    }
    
    @objc private func selectItem(_ gest: UIGestureRecognizer) {
        guard let item = gest.view as? YBCloudComputerPayItem else { return }
        for case let _item as YBCloudComputerPayItem in stackView.arrangedSubviews {
            _item.isSelected = (_item.tag == item.tag)
            
            // 当前的支付方式
            self.selectedTypeBlock?(item.tag)
        }
    }
    
}

// MARK: - YBCloudComputerPayItem
class YBCloudComputerPayItem: UIView {
    
    enum ItemType: Int {
        case wechatPay = 2
        case aliPay = 1
        case apple = 3
        
        var style: (des: String, icon: String) {
            switch self {
            case .wechatPay: return ("微信", "icon_wechatpay")
            case .aliPay: return ("支付宝", "icon_alipay")
            case .apple: return ("苹果内购", "icon_apple")
            }
        }
    }
    
    var isSelected: Bool = false {
        didSet {
            tickBtn.isSelected = isSelected
        }
    }
    
    private let itemType: ItemType
    
    init(type: ItemType) {
        itemType = type
        super.init(frame: .zero)
        
        self.addSubviews([itemBtn, tickBtn])
        tickBtn.snp.makeConstraints { (make) in
            make.size.equalTo(24)
            make.centerY.right.equalToSuperview()
        }
        
        itemBtn.snp.makeConstraints { (make) in
            make.left.centerY.equalToSuperview()
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var itemBtn: BKLayoutButton = {
        let btn = self.bk_addLayoutButton(space: 7.0, bgColor: .clear, imageSize: CGSizeMake(26, 26), image: UIImage(named: itemType.style.icon), titleFont: .systemFont(ofSize: 14, weight: .medium), title: itemType.style.des, titleColor: .lightBlack51DarkLight230)
        btn.isUserInteractionEnabled = false
        return btn
    }()
    
    private lazy var tickBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.isUserInteractionEnabled = false
        btn.setImage(UIImage(named: "icon_check_greentick_normal"), for: .normal)
        btn.setImage(UIImage(named: "icon_check_greentick_select"), for: .selected)
        return btn
    }()
    
}
